package com.apes.auto.car.service;

import com.apes.auto.car.model.CustomCodeRules;
import com.apes.auto.car.repository.CustomCodeRulesRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName CustomCodeRulesService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/8/28 13:46
 * @Version 1.0
 **/
@Service("customCodeRulesService")
public class CustomCodeRulesService extends DomainService {
    @Autowired
    private CustomCodeRulesRepository customCodeRulesRepository;


    public CustomCodeRules save(SimpleRequest request) {
        CustomCodeRules o = request.getO(CustomCodeRules.class);
        return customCodeRulesRepository.saveAndFlush(o);
    }

    public String delete(SimpleRequest request) {
        String id = request.get("id");
        Integer versions = request.get("version");
        CustomCodeRules customCodeRules = request.getO(CustomCodeRules.class);
        if (customCodeRules == null) throw new RuntimeException("自定义规则【" + id + "】在系统中不存在。");
        if (customCodeRules.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        customCodeRulesRepository.delete(customCodeRules);
        return "删除成功";
    }
}
